车型管理
<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="app-container">
      <!-- 搜索栏 -->
      <el-card
        class="search-card-box"
        shadow="never"
        style="margin-bottom: 20px;"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="95px"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="车型编号:"
                style="margin-bottom: 0px"
              >
                <el-input
                  v-model.trim="form.id"
                  clearable
                  placeholder="请输入车型编号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="应载重量:"
                style="margin-bottom: 0px"
              >
                <el-select
                  v-model="form.allowableLoad"
                  placeholder="请选择应载重量"
                >
                  <el-option
                    v-for="item in weightType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="应载体积:"
                style="margin-bottom: 0px"
                label-width="110px"
              >
                <el-select
                  v-model="form.allowableVolume"
                  placeholder="请选择应载体积"
                >
                  <el-option
                    v-for="item in volumeType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="车辆类型:"
                style="margin-top: 20px; margin-bottom: 0px"
              >
                <el-input
                  v-model.trim="form.name"
                  clearable
                  placeholder="请输入车辆类型"
                />
              </el-form-item>
            </el-col>
            <el-col
              :span="8"
              style="margin-top: 20px"
            >
              <el-button
                type="warning"
                @click="searchSubmit"
              >搜索</el-button>
              <el-button
                plain
                class="reset-btn"
                @click="resetForm"
              >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>

      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <!-- 数据渲染 -->
          <el-button
            type="warning"
            class="top"
            @click="add"
          >新增车型</el-button>
          <!-- 数据渲染 -->
          <el-table
            v-loading="isLoding"
            :data="listData"
            stripe
            style="width: 100%;margin-top: 15px"
            class="box"
          >
            <div
              v-show="(!listData || listData.length <= 0) && !listLoading "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong@2x.png"
                alt="img"
                style="margin-top: 20px;width: 25%;height: 25%;"
              >
              <p
                style="
                      margin-top: -20px;
                      padding-bottom: 0px;
                      "
              >这里空空如也</p>
            </div>
            <el-table-column
              label="序号"
              width="70"
              type="index"
              :index="indexFn"
            ></el-table-column>
            <el-table-column
              label="车型编号"
              prop="id"
              min-width="180"
            ></el-table-column>
            <el-table-column
              label="车辆类型"
              prop="name"
              min-width="130"
            ></el-table-column>
            <el-table-column
              label="车型数量"
              prop="num"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="应载重量(吨)"
              prop="allowableLoad"
              min-width="150"
            ></el-table-column>
            <el-table-column
              label="应载体积(立方米)"
              prop="allowableVolume"
              min-width="150"
            ></el-table-column>
            <el-table-column
              label="长(米)"
              prop="measureLong"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="宽(米)"
              prop="measureWidth"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="高(米)"
              prop="measureHigh"
              min-width="100"
            ></el-table-column>
            <el-table-column
              align="center"
              label="操作"
              width="120px"
              class-name="small-padding fixed-width"
              fixed="right"
            >
              <template #default="{row}">
                <el-button
                  type="text"
                  size="small"
                  @click="edit(row.id)"
                >编辑</el-button>
                <el-divider direction="vertical"></el-divider>
                <el-button
                  type="text"
                  size="small"
                  class="col"
                  @click="del(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <div
              class="pages"
            >
              <el-pagination
                :current-page="Number(form.page)"
                :total="Number(total)"
                :page-size="Number(form.pageSize)"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
              />
            </div>
          </div>
        </el-card>
        <!-- 弹窗 -->
        <addCar
          ref="addCar"
          :show-dialog.sync="showDialog"
        ></addCar>
      </div>
    </div>
  </div>
</template>
<script>
import EnumObj from '@/constant/carmodels'
import { list, del, detail } from '@/api/transit'
import addCar from './components/car-models-add.vue'
export default {
  components: {
    addCar
  },
  data() {
    return {
      isLoding: false,
      form: {
        id: null,
        allowableLoad: null,
        allowableVolume: null,
        name: null,
        // 当前页
        page: 1,
        // 当前页容量
        pageSize: 10
      },

      total: 0,
      weightType: EnumObj.weightList,
      volumeType: EnumObj.volumeList,
      listData: [],
      listLoading: false,
      showDialog: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取分页数据,渲染页面
     */
    async getList() {
      this.isLoding = true
      const { data } = await list(this.form)
      this.listData = data.items
      this.form.page = data.page
      this.form.pageSize = data.pageSize
      this.total = data.counts
      this.isLoding = false
    },
    /**
     * 页容量改变
     */
    handleSizeChange(val) {
      this.form.pageSize = val
      this.form.page = 1
      this.getList()
    },
    /**
     * 页码改变
     */
    handleCurrentChange(val) {
      this.form.page = val
      this.getList()
    },
    /**
     * 页码连续问题
     */
    indexFn(val) {
      // 当前页之前 所有的数据总和 + row +1
      return (this.form.page - 1) * this.form.pageSize + (val + 1)
    },
    /**
     * 搜素
     */
    searchSubmit() {
      this.form.page = 1
      this.getList()
    },
    /**
     * 重置
     */
    resetForm() {
      this.form = {
        id: null,
        allowableLoad: null,
        allowableVolume: null,
        name: null,
        // 当前页
        page: 1,
        // 当前页容量
        pageSize: 10
      }
      this.getList()
    },

    /**
     * 编辑车型
     */
    async edit(id) {
      const res = await detail(id)
      this.$refs.addCar.formData = res.data
      this.showDialog = true
    },
    // 新增功能
    async add() {
      this.showDialog = true
    },
    /**
     * 删除功能
     */
    del(id) {
      this.$confirm('确定要删除车型吗', '删除确认')
        .then(async () => {
          const res = await del(id)
          if (res.code === 200) {
            if (this.listData.length === 1 && this.page > 1) {
              this.page--
            }
            this.$message.success('删除成功')
            // 重新渲染数据
            this.getList()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          // console.log('点击了取消')
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.col{
  color: red;
}
.add-btn{
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}
</style>
